package com.building.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.building.common.R;
import com.building.enums.REnum;
import com.building.service.impl.BaseServiceImpl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import com.building.entity.Engineering;
import com.building.mapper.EngineeringMapper;
import com.building.service.EngineeringService;
/**
@author yinjiahui
@create 2021-04-07 19:22
*/
@Service
public class EngineeringServiceImpl extends BaseServiceImpl<EngineeringMapper, Engineering> implements EngineeringService{

    @Override
    public int updateBatch(List<Engineering> list) {
        return baseMapper.updateBatch(list);
    }
    @Override
    public int updateBatchSelective(List<Engineering> list) {
        return baseMapper.updateBatchSelective(list);
    }
    @Override
    public int batchInsert(List<Engineering> list) {
        return baseMapper.batchInsert(list);
    }
    @Override
    public int insertOrUpdate(Engineering record) {
        return baseMapper.insertOrUpdate(record);
    }
    @Override
    public int insertOrUpdateSelective(Engineering record) {
        return baseMapper.insertOrUpdateSelective(record);
    }


    /**
     * 查询所有数据  加 分页
     * @param limit
     * @param page
     * @return
     */
    @Override
    public List<Engineering> selectEngineering(Integer limit,Integer page) {
//        this.engineeringMapper.selectEngineering(limit,page)
        //分页  第一个参数是当前页 第二个参数是每页显示条数
        IPage<Engineering> pageData = new Page<>(limit,page);

        return this.engineeringMapper.selectPage(pageData, null).getRecords();

    }

    /**
     * 根据类别或者阶段进行条件查询   当 engineeringTypeId == 0 时 类别查全部
     *                           当 engineeringStageId == 0 时  阶段查全部
     * @param engineeringTypeId
     * @param engineeringStageId
     * @return
     */
    @Override
    public List<Engineering> selectEngineeringByEngineeringTypeIdOrEngineeringStageId(Integer engineeringTypeId, Integer engineeringStageId) {


        if (engineeringTypeId == 0 && engineeringStageId == 0){//判断类别和阶段是否选择  查询所有

            return this.engineeringMapper.selectList(null);
        }

        if (engineeringTypeId == 0){//判断类别是否选择      查询类别的所有  阶段的选择

            QueryWrapper list = new QueryWrapper();
            list.eq("engineering_type_id",0);
            list.eq("engineering_stage_id",engineeringStageId);

            return this.engineeringMapper.selectList(list);
        }

        if (engineeringStageId == 0){//判断阶段是否选择     查询阶段的所有  类别的选择

            QueryWrapper list = new QueryWrapper();
            list.eq("engineering_type_id",engineeringTypeId);
            list.eq("engineering_stage_id",0);

            return this.engineeringMapper.selectList(list);
        }


        QueryWrapper list = new QueryWrapper();
        list.eq("engineering_type_id",engineeringTypeId);
        list.eq("engineering_stage_id",engineeringStageId);

        return this.engineeringMapper.selectList(list);
    }

    /**
     * 根据项目工程编号查询这条数据       //      回显
     * @param engineeringId
     * @return
     */
    @Override
    public Engineering selectEngineeringByEngineeringId(Integer engineeringId) {
        return this.engineeringMapper.selectById(engineeringId);
    }

    /**
     * 根据项目名称的关键字进行条件查询
     * @param engineeringName
     * @return
     */
    @Override
    public List<Engineering> selectEngineeringByEngineeringName(String engineeringName) {

        QueryWrapper list = new QueryWrapper();
        list.like("engineering_name",engineeringName);
        return this.engineeringMapper.selectList(list);
    }

    /**
     * 根据根据工程编号id删除
     * @param engineeringId
     * @return
     */
    @Override
    public void deleteEngineeringByEngineeringId(Integer engineeringId) {
        this.engineeringMapper.deleteById(engineeringId);
    }

    /**
     * 新增或修改数据
     * @param engineering
     * @return
     */
    @Override
    public R insertOrUpdateEngineering(Engineering engineering) {
        if (engineering.getEngineeringId() == null){//新增
            this.engineeringMapper.insert(engineering);
            return R.result(REnum.SUCCESS_INSERT);
        }else {//修改
            this.engineeringMapper.updateById(engineering);
            return R.result(REnum.SUCCESS_UPDATE);
        }
    }


}
